package com.mikepenz.app.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.amap.api.maps2d.model.LatLng;
import com.mikepenz.app.R;
import com.mikepenz.app.adapter.ListViewAdapter;
import com.mikepenz.presenter.ParkListPresenter;
import com.mikepenz.presenter.imp.ParkListPresenterImp;
import com.mikepenz.view.ParkListView;
import com.quickdv.activity.BaseActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.InjectView;

public class ParkListActivity extends BaseActivity implements ParkListView {

    List<Map<String, Object>> data;
    @InjectView(R.id.toolbar)
    Toolbar toolbar;
    @InjectView(R.id.parklist)
    ListView parklist;
    @InjectView(R.id.park_refresh)
    SwipeRefreshLayout parkRefresh;
    ListViewAdapter adapter;
    ParkListPresenter presenter;
    LatLng latLng;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Handle Toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }


    @Override
    public int getLayoutID() {
        return R.layout.activity_park_list;
    }

    @Override
    public void initView() {

        adapter = new ListViewAdapter(this, data, R.layout.activity_parklist_item, new String[]{"parkName","carCount","managerName"}, new int[]{R.id.park_car_name,R.id.park_car_count_value,R.id.park_car_mileage_value});
        parklist.setAdapter(adapter);

        parklist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                presenter.parkOnClicked(i);
            }
        });
        parkRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (getIntent().getExtras() == null) return;
                presenter.refreshParking(latLng);
            }
        });

        presenter.refreshParking(latLng);
    }

    @Override
    public void initActionbar() {
        getSupportActionBar().setTitle("附近停车场");
    }

    @Override
    public void initData() {
        presenter = new ParkListPresenterImp(this);
        if(getIntent().getExtras()!=null) {
            latLng = new LatLng(getIntent().getExtras().getDouble("lat"), getIntent().getExtras().getDouble("lng"));
        }
        data = new ArrayList<>();
    }

    @Override
    public void refreshParking(List<Map<String, Object>> parks, boolean isRefresh) {
        if(isRefresh){
            data.clear();
        }
        data.addAll(parks);

    }

    @Override
    public void dismissLoad() {
        parkRefresh.setRefreshing(false);
    }

    @Override
    public void showLoad() {
        parkRefresh.setRefreshing(true);
    }

    @Override
    public void notifyDataSetChanged() {
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this,error,Toast.LENGTH_LONG).show();
    }
}
